package com.cote.douhahou.pretloan.status;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import com.cote.douhahou.pretloan.any.util.AkomapalendApRvUtil;
import com.cote.douhahou.pretloan.service.FbackActivity;
import com.cote.douhahou.pretloan.beans.AppAkomapalendConfigCustomize;
import com.cote.douhahou.pretloan.beans.RateNormalWAkomapalendindowConfig;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.utils.AkomapaLendConfigs;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.cote.douhahou.pretloan.R;

public class RateNorDialoAkomapalendg extends Dialog {

    private final String url_akomapalend = "https://play.google.com/store/apps/details?id=" + AkomapaLendConfigs.PackageName;
    private int score_akomapalend = 0;
    private static boolean isShow_akomapalend = false;
    private final ImageButton[] starIbs_akomapalend = new ImageButton[5];

    private final String page_akomapalend = "DIALOG_REVIEW_NORMAL";

    private final FragmentActivity activity_akomapalend;

    public RateNorDialoAkomapalendg(FragmentActivity activity){
        super(activity);
        this.activity_akomapalend = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_rate);

        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(false);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(R.color.transparent);


        findViewById(R.id.drna_btn_notnow).setOnClickListener(v -> {
            HttpHelper.inst().submitEvent(page_akomapalend,"CANCEL_CLICK",String.valueOf(score_akomapalend));
            dismiss();
        });

        TextView textTv = findViewById(R.id.drna_tv_text);

        String content = RateNormalWAkomapalendindowConfig.instance().getContent_akomapalend();
        if (!TextUtils.isEmpty(content)) textTv.setText(content);

        starIbs_akomapalend[0] = findViewById(R.id.drna_ib_star1);
        starIbs_akomapalend[1] = findViewById(R.id.drna_ib_star2);
        starIbs_akomapalend[2] = findViewById(R.id.drna_ib_star3);
        starIbs_akomapalend[3] = findViewById(R.id.drna_ib_star4);
        starIbs_akomapalend[4] = findViewById(R.id.drna_ib_star5);

        findViewById(R.id.drna_btn_rate).setOnClickListener(v -> {

            HttpHelper.inst().submitEvent(page_akomapalend,"RATE_CLICK",String.valueOf(score_akomapalend));

            if (score_akomapalend ==0) {
                Toast.makeText(activity_akomapalend, activity_akomapalend.getString(R.string.tips_please_rate_akomapalend),Toast.LENGTH_SHORT).show();
                return;
            }

            if (score_akomapalend >=4){
                if (AppAkomapalendConfigCustomize.instance().showSysReview_akomapalend()){
                    AkomapalendApRvUtil.INSTANCE.launchRvFlow((AppCompatActivity) activity_akomapalend, aBoolean -> {
                        if (!aBoolean) activity_akomapalend.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_akomapalend)));
                        dismiss();
                        return null;
                    });
                }else activity_akomapalend.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url_akomapalend)));
            }else activity_akomapalend.startActivity(new Intent(activity_akomapalend, FbackActivity.class));
            dismiss();
        });

        for (int i = 0; i< starIbs_akomapalend.length; i++){
            ImageButton scoreIb = starIbs_akomapalend[i];
            final int fScore = i+1;
            if (scoreIb!=null) scoreIb.setOnClickListener(v -> setScore_akomapalend(fScore));
        }
    }


    private void setScore_akomapalend(int score_akomapalend){

        if (score_akomapalend >5) score_akomapalend =5;
        else if (score_akomapalend <1) score_akomapalend =1;

        this.score_akomapalend = score_akomapalend;

        HttpHelper.inst().submitEvent(page_akomapalend,"STAR_CLICK",String.valueOf(score_akomapalend));

        for (int i = 0; i< starIbs_akomapalend.length; i++){
            ImageButton scoreIb = starIbs_akomapalend[i];
            if (scoreIb==null) continue;
            scoreIb.setBackgroundResource(i< score_akomapalend ? R.drawable.icon_star_yellow :R.drawable.icon_star_gray);
        }
    }

    @Override
    public void show() {
        if (isShow_akomapalend) return;
        isShow_akomapalend = true;
        super.show();
        HttpHelper.inst().submitEvent(page_akomapalend,"START","");
        AkomapaLendUtil.addRateCount();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpHelper.inst().submitEvent(page_akomapalend,"END","");
        isShow_akomapalend = false;
    }

}
